package com.msmall.community.common.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.MatchType;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.community.common.dao.SightDao;
import com.msmall.community.common.model.Sight;
import com.msmall.community.common.service.SightManager;
import com.msmall.community.common.util.SightUtil;

public class SightManagerImpl extends BaseManagerImpl<Sight, Long> implements SightManager{

	private static final Log log = LogFactory.getLog(SightManagerImpl.class);
	
	private SightDao sightDao;

	public void setSightDao(SightDao sightDao) {
		this.sightDao = sightDao;
	}

	public void saveSight(File image,String filename, Sight sight, String uat, Long userid) {
		try{
			Sight oldsight = sightDao.loadSightByName(sight.getName());
			if(oldsight != null){
				sight.setImageName(oldsight.getImageName());
				sight.setImagePath(oldsight.getImagePath());
			}
			saveObject(sight);
			if(image != null){
				sight = sightDao.loadSightByName(sight.getName());
				Long sightidx = sight.getSightid();
				String[] pathinfo = SightUtil.saveImage(image, filename, sightidx, uat);
				sight.setImageName(pathinfo[1]);
				sight.setImagePath(pathinfo[0]);
				sightDao.saveObject(sight);
			}
		}catch(Exception e){
			log.error(e);
		}
	}

	public Sight loadSightByBoard(Long boardId) {
		try{
			List<Sight> list = sightDao.getByProperty(Sight.class, "boardId", MatchType.EQ, String.valueOf(boardId));
			if(list != null && list.size() > 0)
				return list.get(0);
			return null;
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}
}
